<template>
	<mescroll-uni :ref="'mescrollRef'+i" @init="mescrollInit" @down="downCallback" @up="upCallback">
		<view>
			<view v-if="i === 0">
				<view v-for="(item, index) in dataList" :key="index" style="padding: 0 14rpx;width: 100%;">
					<view v-if="[5, 7].includes(item.goodsType)">
						<coupon-card :item="({ ...item, couponType: 3, canBuy: true })"></coupon-card>
					</view>
					<commodity-card v-else :item="item"></commodity-card>
				</view>
			</view>

			<view v-if="i === 1">
				<view v-for="(item, index) in dataList" :key="index" class="flex-center" style="padding: 0 14rpx;"
					@click="jumpUrl(`/pages/user/comerciante/detail/detail?merchantId=${ item.merchantId }`)">
					<shop-card :item="item" @phone="$emit('phone')"></shop-card>
				</view>
			</view>

			<view v-if="i === 2" class="flex-between" style="padding: 20rpx 30rpx 0;">
				<view v-for="(list, lindex) in groups" :key="lindex" :class="[lindex > 0 ? 'u-p-l-18' : '']"
					style="width: 100%;">
					<view v-for="(item, index) in list" :key="index" style="width: 100%"
						@click="jumpUrl(`/pages/other/comment/note?notesId=${ item.notesId }`)">
						<dynamic-card :item="item"></dynamic-card>
					</view>
				</view>
			</view>
		</view>
	</mescroll-uni>
</template>

<script>
	import {
		userGoodsCollectPage,
		userMerchantCollectPage,
		userNotesCollectPage
	} from '@/api/personal.js'
	import waterfall from '@/utils/waterfall.js'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import MescrollMoreItemMixin from "@/components/mescroll-uni/mixins/mescroll-more-item.js";

	export default {
		mixins: [MescrollMixin, MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
		props: {
			i: Number, // 每个tab页的专属下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			index: { // 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
				type: Number,
				default () {
					return 0
				}
			},
			updateStatus: {
				default: ''
			}
		},
		data() {
			return {
				dataList: [],

				leftRightHeight: [0, 0],
				groups: [
					[],
					[]
				]
			}
		},
		watch: {
			updateStatus(v) {
				if (!v) {
					this.downCallback()
				}
			}
		},
		methods: {
			getList(obj) {
				let index = this.i
				if (index === 0) {
					return userGoodsCollectPage(obj)
				} else if (index === 1) {
					return userMerchantCollectPage(obj)
				} else if (index === 2) {
					return userNotesCollectPage(obj)
				}
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				let node = this.$refs['mescrollRef' + this.i]
				this.getList({
					pageNumber: page.num,
					pageSize: page.size
				}).then(res => {

					if (this.i === 2) {
						if (page.num == 1) {
							this.groups = [
								[],
								[]
							]
							this.leftRightHeight = [0, 0]
						}; //如果是第一页需手动制空列表
						waterfall({
							hs: this.leftRightHeight,
							list: res.list,
							key: 'imgUrls'
						}, (hs, e) => {
							node && node.mescroll.endSuccess(res.list.length);
							this.leftRightHeight = hs
							this.groups = [
								[...this.groups[0], ...e[0]],
								[...this.groups[1], ...e[1]]
							]
						})
					} else {
						node && node.mescroll.endSuccess(res.list.length);
						if (page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
						this.dataList = this.dataList.concat(res.list); //追加新数据
					}
				}).catch((e) => {
					node && node.mescroll.endErr();
				})
			},
			downCallback() {
				let node = this.$refs['mescrollRef' + this.i]
				node && node.mescroll.resetUpScroll()
			},
		}
	}
</script>
